
#pragma once

#include <list>
#include "buffer.h"

namespace serial {

class Parser {
public:
    Parser();
    virtual ~Parser();

    class FSM {
    public:
        FSM() {}
        virtual ~FSM() {}
        // > 0 : success
        // = 0 : need more
        // < 0 : failed.
        virtual int onData(unsigned char data) = 0;
        virtual void reset() = 0;
    };

    // > 0: success. length of data.
    // = 0: call parse(NULL, 0, ...) again.
    // < 0: nothing parsed.
    int parse(const char * buf, int len, unsigned char * outbuf, int outlen, const FSM** pfsm = 0);

    void addFsm(FSM* fsm);
    void resetAllFsms();
private:
    std::list<FSM*> mFsms;
    Buffer  mBuffer;
};

} // namespace serial


